package com.example.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.Result;
import com.example.entry.OrderForm;
import com.example.service.IOrderFormService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/*
 * 订单前台控制器
 * */
@RestController
@RequestMapping("/web/order")
public class OrderFormWebController {

    @Resource
    private IOrderFormService orderFormService;

    /*
    * 用户普通下单
    * */
    @PostMapping("/buy")
    public Result userBuyProduct(@RequestBody OrderForm orderForm){
        Integer i = orderFormService.userBuyProduct(orderForm);
        return Result.success("购买成功", i);
    }

    /*
    * 用户使用支付宝下单
    * */
    @PostMapping("/alipay-payment")
    public Result alipayPayment(@RequestBody OrderForm orderForm){
        Integer integer = orderFormService.useAlipayPayment(orderForm);
        if(integer == 0){
            return Result.error("订单生成失败");
        }
        return Result.success("订单生成成功", integer);
    }

    /*
    * 用户查询订单
    * */
    @GetMapping("/find-order/list")
    public Result getUserAllOrder(@RequestParam(defaultValue = "") Integer status,
                                  @RequestParam Integer userId){
        QueryWrapper<OrderForm> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(status)){
            wrapper.eq("status", status);
        }

        wrapper.eq("user_id", userId);
        List<OrderForm> list = orderFormService.list(wrapper);
        return Result.success("查询成功", list);
    }

    /*
     * 用户取消订单
     * */
    @PostMapping("/cancel-order")
    public Result userCancelOrder(@RequestBody OrderForm orderForm){
        orderFormService.userCancelOrder(orderForm); // 调用service接口
        return Result.success("取消成功");
    }

    /*
    * 用户删除订单
    * */
    @GetMapping("/del-order/id={id}")
    public Result userDelereOrder(@PathVariable Integer id){
        orderFormService.removeById(id);
        return Result.success("删除成功");
    }

}
